﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WXYW.Entities;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW.BLL
{
    public class ReturnToFactoryService : RepositoryBase
    {
        public List<ReturnToFactoryForm> GetAllForms()
        {
            List<ReturnToFactoryForm> list = GetEntities<ReturnToFactoryForm>().OrderByDescending(f => f.ApplyDate).ToList();
            foreach (ReturnToFactoryForm form in list)
            {
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
            }

            return list;
        }

        public List<ReturnToFactoryForm> GetAllForms(string keyword, int pageIndex, int pageSize)
        {
            IQueryable<ReturnToFactoryForm> list;
            
            if (!string.IsNullOrEmpty(keyword))
            {
                list = GetEntities<ReturnToFactoryForm>(r =>
                       r.RunningNumber.Contains(keyword) || r.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword)
                    || r.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword)
                    || r.ApplicationProduct.ProductSerialNumber.Contains(keyword)
                    || r.ApplicationProduct.RepairAccessory.Contains(keyword)
                    || r.ApplicationProduct.ProblemType.Name.Contains(keyword)
                    || r.Status.Equals(keyword)).OrderByDescending(f => f.ApplyDate).Skip(pageIndex * pageSize).Take(pageSize + 1);
            }
            else
            {
                list = GetEntities<ReturnToFactoryForm>().OrderByDescending(f => f.ApplyDate).Skip(pageIndex * pageSize).Take(pageSize + 1);
            }

            foreach (ReturnToFactoryForm form in list)
            {
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
            }

            return list.ToList();
        }

        public int GetCountOfReturnToFactoryForms(string keyword)
        {
            IQueryable<ReturnToFactoryForm> list;

            if (!string.IsNullOrEmpty(keyword))
            {
                list = GetEntities<ReturnToFactoryForm>(r =>
                       r.RunningNumber.Contains(keyword) || r.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword)
                    || r.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword)
                    || r.ApplicationProduct.ProductSerialNumber.Contains(keyword)
                    || r.ApplicationProduct.RepairAccessory.Contains(keyword)
                    || r.ApplicationProduct.ProblemType.Name.Contains(keyword)
                    || r.Status.Equals(keyword));
            }
            else
            {
                list = GetEntities<ReturnToFactoryForm>();
            }
            return list.Count();
        }

        public List<ReturnToFactoryForm> GetReturnToFactoryFormByStatus(string status, User currentUser)
        {
            IQueryable<ReturnToFactoryForm> rtfFoms = GetEntities<ReturnToFactoryForm>(f => f.Status == status).OrderBy(f => f.ApplyDate);
            if (currentUser.UserType.ID == (int)RoleType.PlatformEngineer || currentUser.UserType.ID == (int)RoleType.Engineer)
            {
                rtfFoms = rtfFoms.Where(r => r.ApplicationProduct.User.ServiceStation.ID == currentUser.ServiceStation.ID);
            }
            List<ReturnToFactoryForm> list = rtfFoms.ToList();
            foreach (ReturnToFactoryForm form in list)
            {
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                int overDays = (DateTime.Now.Date - form.ApplicationProduct.ApplicationForm.ApplicationDateTime.Value.Date).Days;
                if (overDays < 4)
                {
                    form.Overdue = "!" + overDays.ToString();
                }
                else if (overDays < 7)
                {
                    form.Overdue = "!!" + overDays.ToString();
                }
                else
                {
                    form.Overdue = "!!!" + overDays.ToString();
                }
            }
            return list;
        }

        public List<ReturnToFactoryForm> GetReturnToFactoryFormToReceive()
        {
            List<ReturnToFactoryForm> list = GetEntities<ReturnToFactoryForm>(f => f.Status.Equals("返厂-待收货确认")).OrderBy(f => f.ApplyDate).ToList();
            foreach (ReturnToFactoryForm form in list)
            {
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                int overDays = (DateTime.Now.Date - form.ApplicationProduct.ApplicationForm.ApplicationDateTime.Value.Date).Days;
                if (overDays < 4)
                {
                    form.Overdue = "!" + overDays.ToString();
                }
                else if (overDays < 7)
                {
                    form.Overdue = "!!" + overDays.ToString();
                }
                else
                {
                    form.Overdue = "!!!" + overDays.ToString();
                }
                form.Status = "返厂-待收货确认";

                if (!form.ConfirmQuatationDate.HasValue && form.ApplicationProduct.ConfirmQuotationDateTime.HasValue)
                {
                    form.ConfirmQuatationDate = form.ApplicationProduct.ConfirmQuotationDateTime;
                }
            }
            return list.OrderByDescending(f=>f.ID).ToList();
        }


        public void AddRetrunToFactoryForm(long applicatinProductID, string blxx, string requirement, string runningNO,
            string checkResult)
        {
            ApplicationProductService productService = new ApplicationProductService();
            ApplicationProduct product = productService.GetApplicationProductByID(applicatinProductID);
            product.Status = "返厂维修中";
            product.BLXX = blxx;
            product.Remark = checkResult;

            ReturnToFactoryForm form = new ReturnToFactoryForm();
            form.ApplicationProduct = product;
            form.ApplyDate = DateTime.Now;
            form.Status = "返厂-待确认厂商";
            form.Requirement = requirement;
            form.RunningNumber = runningNO;
            InsertEntity(form);
        }

        public ReturnToFactoryForm GetReturnToFactoryFormProductDetail(long formID)
        {
            ReturnToFactoryForm form = GetReturnToFactoryFormByID(formID);
            if (!form.ApplicationProductReference.IsLoaded)
            {
                form.ApplicationProductReference.Load();
            }
            if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                form.ApplicationProduct.ProductTypeReference.Load();
            }
            if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
            {
                form.ApplicationProduct.ApplicationFormReference.Load();
            }
            if (!form.ApplicationProduct.ApplicationForm.UserReference.IsLoaded)
            {
                form.ApplicationProduct.ApplicationForm.UserReference.Load();
            }
            if (!form.ReturnToFactoryQuatation.IsLoaded)
            {
                form.ReturnToFactoryQuatation.Load();
            }
            if (!form.ApplicationProduct.UserReference.IsLoaded)
            {
                form.ApplicationProduct.UserReference.Load();
            }
            if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
            {
                form.ApplicationProduct.ProductTypeReference.Load();
            }
            //if (!form.ApplicationProduct.ApplicationForm.ProviderReference.IsLoaded)
            //{
            //    form.ApplicationProduct.ApplicationForm.ProviderReference.Load();
            //}

            return form;
        }

        public ReturnToFactoryForm GetReturnToFactoryFormByID(long formID)
        {
            return GetEntities<ReturnToFactoryForm>(r => r.ID == formID).FirstOrDefault();
        }

        public int GetNumberOfReturnToFactoryForms(string rtfCode)
        {
            return GetEntities<ReturnToFactoryForm>(r => r.RunningNumber.Contains(rtfCode)).Count();
        }

        public void Update(ReturnToFactoryForm form)
        {
            UpdateEntity(form);
        }

        public List<ReturnToFactoryForm> GetReturnToFactoryFormsByKeywords(string keyword)
        {
            List<ReturnToFactoryForm> formList = new List<ReturnToFactoryForm>();
            //IQueryable<ReturnToFactoryForm> formsQueryable = GetEntities<ReturnToFactoryForm>();
            if (!string.IsNullOrEmpty(keyword))
            {
                formList = GetEntities<ReturnToFactoryForm>(r =>
                       r.RunningNumber.Contains(keyword) || r.ApplicationProduct.ApplicationForm.ApplicationCode.Contains(keyword)
                    || r.ApplicationProduct.ProductType.ProductTypeName.Contains(keyword)
                    || r.ApplicationProduct.ProductSerialNumber.Contains(keyword)
                    || r.ApplicationProduct.RepairAccessory.Contains(keyword)
                    || r.ApplicationProduct.ProblemType.Name.Contains(keyword)
                    || r.Status.Equals(keyword)).ToList();
            }
            else
            {
                formList = GetEntities<ReturnToFactoryForm>().ToList();
            }

            foreach (ReturnToFactoryForm form in formList)
            {
                if (!form.ApplicationProductReference.IsLoaded)
                {
                    form.ApplicationProductReference.Load();
                }
                if (!form.ApplicationProduct.ApplicationFormReference.IsLoaded)
                {
                    form.ApplicationProduct.ApplicationFormReference.Load();
                }
                if (!form.ApplicationProduct.ProductTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProductTypeReference.Load();
                }
                if (!form.ApplicationProduct.ProblemTypeReference.IsLoaded)
                {
                    form.ApplicationProduct.ProblemTypeReference.Load();
                }
            }

            return formList;
        }

        public IQueryable<ReturnToFactoryQuatation> GetQuatationList(long formID)
        {
            return GetEntities<ReturnToFactoryQuatation>().Where(q => q.ReturnToFactoryForm.ID == formID);
        }

        public void AddQuatation(ReturnToFactoryQuatation quatationItem)
        {
            InsertEntity(quatationItem);
        }

        public void RemoveQuatation(long quatationID)
        {
            DeleteEntities<ReturnToFactoryQuatation>(q => q.ID == quatationID, Guid.Empty);
        }

        public List<RepairOverdueModel> GetFCOverdueProduct()
        {
            List<RepairOverdueModel> models = new List<RepairOverdueModel>();
            List<ReturnToFactoryForm> list = GetEntities<ReturnToFactoryForm>(f => f.FactoryReceiveDate.HasValue).ToList();
            foreach (ReturnToFactoryForm form in list)
            {
                if (DateTimeService.CalculateDateTimeSpan(form.FactoryReceiveDate.Value, DateTime.Now) > 2)
                {
                    if (!form.ApplicationProductReference.IsLoaded)
                    {
                        form.ApplicationProductReference.Load();
                    }
                    if (!form.ApplicationProduct.UserReference.IsLoaded)
                    {
                        form.ApplicationProduct.UserReference.Load();
                    }
                    RepairOverdueModel overdueModel = new RepairOverdueModel();
                    overdueModel.EngineerEmail = form.ApplicationProduct.User.UserEmail;
                    overdueModel.EngineerName = form.ApplicationProduct.User.ContactPerson;
                    overdueModel.OverdueDay = DateTimeService.CalculateDateTimeSpan(form.FactoryReceiveDate.Value, DateTime.Now);
                    overdueModel.RMACode = form.Code;
                    models.Add(overdueModel);
                }
            }
            return models;
        }
    }
}
